import { Injectable } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { UserService } from 'src/database/user/user.service'

@Injectable()
export class AuthService {
    constructor(
        private readonly userService: UserService,
        private readonly jwtService: JwtService
    ) { }

    async validateUser(username: string, pass: string): Promise<any> {
        const user = await this.userService.findOne(username)
        console.log(user);
        if (user && user.password === pass) {
            const { password, ...result } = user
            return result
        }
    
        return null
    }

    async login(user: any) {
        const payload = { username: user.username, sub: user.uid }
        return {
            token: this.jwtService.sign(payload),
        }
    }
}
